<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>后台管理系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 8px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s;
            background-color: #f8f9fa;
        }

        .form-input:focus {
            outline: none;
            border-color: #667eea;
            background-color: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-input.error {
            border-color: #e74c3c;
        }

        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .error-message.show {
            display: block;
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 10px;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .login-btn:active {
            transform: translateY(0);
        }

        .login-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 8px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .message {
            margin-top: 15px;
            padding: 12px;
            border-radius: 6px;
            text-align: center;
            font-size: 14px;
            display: none;
        }

        .message.success {
            background-color: #d4edda;
            color: #155724;
            display: block;
        }

        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            display: block;
        }

        .login-footer {
            text-align: center;
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #e1e5e9;
        }

        .login-footer p {
            color: #666;
            font-size: 12px;
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
            }

            .login-header h1 {
                font-size: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>后台管理系统</h1>
            <p>请输入您的账号密码登录</p>
        </div>

        <form id="loginForm">
            <div class="form-group">
                <label class="form-label" for="username">用户名</label>
                <input type="text" id="username" class="form-input" placeholder="请输入用户名" required>
                <div class="error-message" id="usernameError">请输入用户名</div>
            </div>

            <div class="form-group">
                <label class="form-label" for="password">密码</label>
                <input type="password" id="password" class="form-input" placeholder="请输入密码" required>
                <div class="error-message" id="passwordError">请输入密码</div>
            </div>

            <button type="submit" class="login-btn" id="loginBtn">
                <span id="loginText">登录</span>
                <span id="loadingSpinner" class="loading" style="display: none;"></span>
            </button>

            <div id="message" class="message"></div>
        </form>

        <div class="login-footer">
            <p>© 2024 后台管理系统</p>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const loginForm = document.getElementById('loginForm');
            const usernameInput = document.getElementById('username');
            const passwordInput = document.getElementById('password');
            const loginBtn = document.getElementById('loginBtn');
            const loginText = document.getElementById('loginText');
            const loadingSpinner = document.getElementById('loadingSpinner');
            const messageDiv = document.getElementById('message');
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');

            // 表单验证
            function validateForm() {
                let isValid = true;
                
                // 重置错误状态
                usernameInput.classList.remove('error');
                passwordInput.classList.remove('error');
                usernameError.classList.remove('show');
                passwordError.classList.remove('show');

                // 验证用户名
                if (!usernameInput.value.trim()) {
                    usernameInput.classList.add('error');
                    usernameError.classList.add('show');
                    isValid = false;
                }

                // 验证密码
                if (!passwordInput.value.trim()) {
                    passwordInput.classList.add('error');
                    passwordError.classList.add('show');
                    isValid = false;
                }

                return isValid;
            }

            // 显示加载状态
            function setLoading(isLoading) {
                if (isLoading) {
                    loginBtn.disabled = true;
                    loginText.style.display = 'none';
                    loadingSpinner.style.display = 'inline-block';
                } else {
                    loginBtn.disabled = false;
                    loginText.style.display = 'inline-block';
                    loadingSpinner.style.display = 'none';
                }
            }

            // 显示消息
            function showMessage(message, type) {
                messageDiv.textContent = message;
                messageDiv.className = 'message ' + type;
                
                // 5秒后自动消失
                setTimeout(() => {
                    messageDiv.className = 'message';
                }, 5000);
            }

            // 登录处理
            loginForm.addEventListener('submit', function (event) {
                event.preventDefault();
                
                if (!validateForm()) {
                    return;
                }

                setLoading(true);

                const loginData = {
                    username: usernameInput.value.trim(),
                    password: passwordInput.value.trim()
                };

                // 发送登录请求
                fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(loginData)
                })
                .then(response => response.json())
                .then(data => {
                    setLoading(false);
                    
                    if (data.code === 200) {
                        showMessage('登录成功，正在跳转...', 'success');
                        
                        // 保存用户信息到localStorage
                        localStorage.setItem('userInfo', JSON.stringify(data.data));
                        
                        // 根据用户权限跳转到不同页面
                        setTimeout(() => {
                            // 检查用户角色和推广码
                            const userData = data.data;
                            if (userData.isAdmin === 1) {
                                // 管理员跳转到完整管理页面
                                window.location.href = 'admin.html';
                            } else if (userData.promotionCode || userData.role === 'promoter') {
                                // 有推广码或角色为推广员，跳转到推广员中心
                                window.location.href = 'promoter.html';
                            } else {
                                // 普通用户跳转到用户管理页面
                                window.location.href = 'user-management.html';
                            }
                        }, 1000);
                    } else {
                        showMessage('登录失败: ' + (data.message || '用户名或密码错误'), 'error');
                    }
                })
                .catch(error => {
                    setLoading(false);
                    showMessage('网络错误，请重试', 'error');
                    console.error('Login error:', error);
                });
            });

            // 实时验证输入
            usernameInput.addEventListener('input', function() {
                if (this.value.trim()) {
                    this.classList.remove('error');
                    usernameError.classList.remove('show');
                }
            });

            passwordInput.addEventListener('input', function() {
                if (this.value.trim()) {
                    this.classList.remove('error');
                    passwordError.classList.remove('show');
                }
            });

            // 按回车键提交表单
            document.addEventListener('keydown', function(event) {
                if (event.key === 'Enter') {
                    loginForm.dispatchEvent(new Event('submit'));
                }
            });
        });
    </script>
</body>
</html>
